package cn.iocoder.yudao.module.promotion.dal.mysql.yearcardorder;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.promotion.controller.admin.yearcardorder.vo.YearCardOrderPageReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.yearcardorder.YearCardOrderDO;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * 年卡订单 Mapper
 *
 * @author 万民匠心
 */
@Mapper
public interface YearCardOrderMapper extends BaseMapperX<YearCardOrderDO> {

    default PageResult<YearCardOrderDO> selectPage(YearCardOrderPageReqVO reqVO) {
        LambdaQueryWrapperX<YearCardOrderDO> yearCardOrderDOLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        yearCardOrderDOLambdaQueryWrapperX
                .eqIfPresent(YearCardOrderDO::getOrderNo, reqVO.getOrderNo())
                .eqIfPresent(YearCardOrderDO::getMemberId, reqVO.getMemberId())
                .likeIfPresent(YearCardOrderDO::getNickname, reqVO.getNickname())
                .eqIfPresent(YearCardOrderDO::getMobile, reqVO.getMobile())
                .eqIfPresent(YearCardOrderDO::getYearCardAmount, reqVO.getYearCardAmount())
                .eqIfPresent(YearCardOrderDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(YearCardOrderDO::getCreateTime, reqVO.getCreateTime())
                .eq(YearCardOrderDO::getPayOrderStatus, 1)
                .orderByDesc(YearCardOrderDO::getId);
        if (reqVO.getStatus() != null) {
            if (reqVO.getStatus()) {
                yearCardOrderDOLambdaQueryWrapperX.ltIfPresent(YearCardOrderDO::getYearCardStartTime, LocalDateTime.now());
                yearCardOrderDOLambdaQueryWrapperX.gtIfPresent(YearCardOrderDO::getYearCardEndTime, LocalDateTime.now());
            } else {
                yearCardOrderDOLambdaQueryWrapperX.gtIfPresent(YearCardOrderDO::getYearCardStartTime, LocalDateTime.now());
                yearCardOrderDOLambdaQueryWrapperX.ltIfPresent(YearCardOrderDO::getYearCardEndTime, LocalDateTime.now());
            }
        }
        PageResult<YearCardOrderDO> result = selectPage(reqVO, yearCardOrderDOLambdaQueryWrapperX);
        result.getList().forEach(e -> {
            if (Objects.equals(e.getStatus(), Boolean.TRUE) && e.getYearCardStartTime() != null && e.getYearCardEndTime() != null
                    && (e.getYearCardStartTime().isAfter(LocalDateTime.now()) || e.getYearCardEndTime().isBefore(LocalDateTime.now()))) {
                e.setStatus(Boolean.FALSE);
            }
        });
        return result;
    }

    default List<YearCardOrderDO> selectExpireOrder() {
        LambdaQueryWrapperX<YearCardOrderDO> yearCardOrderDOLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        yearCardOrderDOLambdaQueryWrapperX
                .and(ww -> ww.eq(YearCardOrderDO::getStatus, Boolean.TRUE).eq(YearCardOrderDO::getPayOrderStatus, 1))
                .and(ww -> ww.gt(YearCardOrderDO::getYearCardStartTime, LocalDateTime.now())
                        .or().lt(YearCardOrderDO::getYearCardEndTime, LocalDateTime.now()));
        return selectList(yearCardOrderDOLambdaQueryWrapperX);
    }

    default List<YearCardOrderDO> selectNotExpireOrder() {
        return selectList(new LambdaQueryWrapperX<YearCardOrderDO>()
                .eq(YearCardOrderDO::getStatus, Boolean.TRUE)
                .eq(YearCardOrderDO::getPayOrderStatus, 1)
                .lt(YearCardOrderDO::getYearCardStartTime, LocalDateTime.now())
                .gt(YearCardOrderDO::getYearCardEndTime, LocalDateTime.now()));
    }

}